package com.cswangdao.controller.admin;

import com.cswangdao.domain.dto.TopicPageDTO;
import com.cswangdao.domain.model.MarketTopic;
import com.cswangdao.domain.vo.TopicDetailVo;
import com.cswangdao.result.PageResult;
import com.cswangdao.result.Result;
import com.cswangdao.service.TopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/topic")
@RestController
@Slf4j
public class AdminTopicController {

    @Autowired
    private TopicService topicService;

    @GetMapping("/list")
    public Result<PageResult<MarketTopic>> list(TopicPageDTO pageDTO) {
        log.info("话题列表查询：{}", pageDTO);
        PageResult<MarketTopic> pageResult = topicService.pageQueryInfo(pageDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/read")
    public Result<TopicDetailVo> read(String id) {
        log.info("话题详情查询：{}", id);
        TopicDetailVo topicDetailVo = topicService.queryById(id);
        return Result.success(topicDetailVo);
    }

    @PostMapping("/update")
    public Result<MarketTopic> update(@RequestBody MarketTopic topic) {
        log.info("话题更新：{}", topic);
        topicService.update(topic);
        return Result.success(topic);
    }

    @PostMapping("/create")
    public Result<MarketTopic> create(@RequestBody MarketTopic topic) {
        log.info("话题创建：{}", topic);
        topicService.addTopic(topic);
        return Result.success(topic);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody MarketTopic topic) {
        topicService.delete(topic.getId());
        return Result.success();
    }


    @PostMapping("/batch-delete")
    public Result batchDelete(@RequestBody Map<String,Integer[]> map) {
        log.info("批量删除：{}", map);
        List<Integer> ids = Arrays.asList(map.get("ids"));
        topicService.batchDelete(ids);
        return Result.success();
    }

}
